import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'generated/l10n.dart';
import 'models/appData.dart';
import 'routers/routers.dart';
import 'utils/application.dart';
import 'utils/constants.dart';
import 'utils/sizeFit.dart';

void main() {
  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider<AppLocale>(create: (_) => AppLocale()),
      ChangeNotifierProvider<AppThemeMode>(create: (_) => AppThemeMode()),
    ],
    child: const MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // 在全局或顶层State中声明
  final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

  // 初始化路由
  void initRouter() {
    // 创建全局路由实例
    FluroRouter router = FluroRouter();
    // 路由注册
    defineRoutes(router);
    // 将路由对象静态化
    Application.router = router;
  }

  @override
  Widget build(BuildContext context) {
    // 初始化sizefit
    SizeFit.initialize(context);
    // 初始化路由
    initRouter();

    return MaterialApp(
      title: 'Flutter Demo',
      navigatorKey: navigatorKey, // 绑定Key
      theme: lightThemeData,
      darkTheme: darkThemeData,
      themeMode: context.watch<AppThemeMode>().themeMode,
      initialRoute: '/',
      onGenerateRoute: Application.router!.generator,
      locale: context.watch<AppLocale>().locale, // 设置当前的 Locale
      localizationsDelegates: const [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale('en', 'US'), //英文
        Locale('zh', 'CN'), //简体中文
      ],
    );
  }
}
